package com.lagou.zk.zkconfigure.zkclient;

import com.alibaba.fastjson.JSON;
import org.I0Itec.zkclient.ZkClient;

/**
 * 注册配置
 */
public class RegisterConf {
    //zk地址
    public static String ZKURL="127.0.0.1:2181";
    //zk节点
    public static String ZKNODE="/lg_zkconf";

    public static void main(String[] args) throws InterruptedException {
        //String url="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC";
        String url="jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=UTC";
        String username="root";
        String password="root";

        PoolConf registerConf = new PoolConf(url, username, password);
        String jsonStr = JSON.toJSONString(registerConf);

        //1.创建zkclient
        ZkClient zkClient = new ZkClient(ZKURL);
        boolean exists = zkClient.exists(ZKNODE);
        if(!exists){
            //创建节点
            zkClient.createPersistent(ZKNODE,jsonStr);
            Thread.sleep(1000);
        }else{
            // 更新节点内容
            zkClient.writeData(ZKNODE,jsonStr);
            Thread.sleep(1000);
        }

    }

}
